/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.TabbedDialog;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A class representing creation dialog on screen.
 * This dialog is provided by some tabs like limits,
 * grid and General settings.
 * For example:
 * <pre>
 *    CreateSettingsDialog dgl = new CreateSettingsDialog(parent, this);
 *    dgl.pack();
 * </pre>
 *
 * @author Pitangueira, Roque & Brugiolo, Marco & Fonseca, Flavio
 * @version 0.0.1 Beta
 * @see gui.InsaneInternalInterface
 * @since jdk1.4.1
 */
public final class ElementTypeDialog extends TabbedDialog {
	
	private InsaneInternalInterface parent;
	private static final long serialVersionUID = 1L;
    private JPanel LSPane = new JPanel();
    private JRadioButton lagran = new JRadioButton();
    private JRadioButton serend = new JRadioButton();
	
	private JPanel TQPane = new JPanel();
	private JRadioButton typeQ = new JRadioButton();
	private JRadioButton typeT = new JRadioButton();
	private JCheckBox linearDv = new JCheckBox();
	
    private JPanel nodesNumberPane = new JPanel();
    private JPanel nodesNumberPane1 = new JPanel();
    private JPanel nodesNumberPane2 = new JPanel();
    private DecimalField nodesNumber;
	
    private DecimalField eNumber;
	
	private NumberFormat numberFormat;
	
	private int nCurves;
	
	private JLabel labelTQ;
	
	private String eTypeLS;
	private String eTypeTQ;
	private double eTypeNN;
	private double eTypeNE;
	
    /**
     * Constructor method, calling super with the especific title.
     *
     * @param parent The parent of the class.
     * @see gui.InsaneInternalInterface
     */
    public ElementTypeDialog(InsaneInternalInterface parent, int ncvs, String typeLS, String typeTQ, boolean div, int npts, int netd, int lb) {
		
    	this.parent = parent;
    	
    	lagran.setText(parent.getResourceBundle().getString("dElmTypeDialog01"));
        serend.setText(parent.getResourceBundle().getString("dElmTypeDialog02"));
        typeQ.setText(parent.getResourceBundle().getString("dElmTypeDialog03"));
    	typeT.setText(parent.getResourceBundle().getString("dElmTypeDialog04"));
    	linearDv.setText(parent.getResourceBundle().getString("dElmTypeDialog05"));
    	
		nCurves = ncvs;
		eTypeLS = typeLS;
		eTypeTQ = typeTQ;
		eTypeNN = npts;
		eTypeNE = netd;
		
        setUpFormat();
		
        // Sets up the view panel
        JPanel meshPane = new JPanel();
        meshPane.setLayout(new BoxLayout(meshPane, BoxLayout.Y_AXIS));
		
        // Sets up the view panel
        JPanel elementType = new JPanel();
        elementType.setLayout(new BoxLayout(elementType, BoxLayout.X_AXIS));
        
		LSPane.setLayout(new GridLayout(2,1,2,2));
        LSPane.setMaximumSize(new Dimension(500,160));
        LSPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmTypeDialog06")),
        BorderFactory.createEmptyBorder(2,2,2,2)));
        LSPane.add(lagran);
        LSPane.add(serend);
        if ( eTypeLS == "Lagrange" )
			lagran.setSelected(true);
		else
			serend.setSelected(true);
		ButtonGroup g1 = new ButtonGroup();
		g1.add(lagran);
		g1.add(serend);
        elementType.add(LSPane);
		
		if ( nCurves != 3 )
			elementType.add(TQPane);
		else
			eTypeTQ = "Triangulate";
		
		TQPane.setLayout(new GridLayout(3,1,2,2));
        TQPane.setMaximumSize(new Dimension(500,160));
        TQPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmTypeDialog07")),
        BorderFactory.createEmptyBorder(2,2,2,2)));
        TQPane.add(typeQ);
        TQPane.add(typeT);
        TQPane.add(linearDv);
        if ( eTypeTQ == "Triangulate" ) {
			typeT.setSelected(true);
			labelTQ = new JLabel(parent.getResourceBundle().getString("dElmTypeDialog08"));
			linearDv.setSelected(div);
			linearDv.setEnabled(true);
		} else {
			typeQ.setSelected(true);
			labelTQ = new JLabel(parent.getResourceBundle().getString("dElmTypeDialog09"));
			linearDv.setSelected(false);
			linearDv.setEnabled(false);
		}
    	ButtonGroup g2 = new ButtonGroup();
		g2.add(typeT);
		g2.add(typeQ);
		
        elementType.add(nodesNumberPane);
        nodesNumberPane.setLayout(new BoxLayout(elementType, BoxLayout.X_AXIS));
        nodesNumberPane.setLayout(new GridLayout(1,4,2,2));
        nodesNumberPane.setMaximumSize(new Dimension(500,150));
        nodesNumberPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmTypeDialog10")),
        BorderFactory.createEmptyBorder(2,2,2,2)));
		
        JPanel nodesNumberPane0 = new JPanel();
        nodesNumberPane0.setLayout(new BoxLayout(nodesNumberPane0, BoxLayout.X_AXIS));
        nodesNumberPane.add(nodesNumberPane0);
		
		//DEITEL
		// register events for JRadioButtons
		RadioButtonHandler handler = new RadioButtonHandler();
		typeT.addItemListener( handler );
		typeQ.addItemListener( handler );
		//End of DEITEL
		
        nodesNumberPane0.add(nodesNumberPane1);
        nodesNumberPane1.setLayout(new GridLayout(2,1,2,2));
        nodesNumberPane1.setMaximumSize(new Dimension(100,100));
        nodesNumberPane1.add(new JLabel("  "));
        nodesNumberPane1.add(labelTQ);
		setSize( 30, 60 );
		
        nodesNumberPane0.add(nodesNumberPane2);
        nodesNumberPane2.setLayout(new GridLayout(2,1,2,2));
        nodesNumberPane2.setMaximumSize(new Dimension(200,200));
        nodesNumberPane2.add(new JLabel(parent.getResourceBundle().getString("dElmTypeDialog11")));
		nodesNumber  = new DecimalField(eTypeNN, 2, numberFormat);
        nodesNumberPane2.add(nodesNumber);
		
        // Sets up the view panel
        JPanel loftingRegion = new JPanel();
        loftingRegion.setLayout(new BoxLayout(loftingRegion, BoxLayout.Y_AXIS));
        loftingRegion.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmTypeDialog12")),
        BorderFactory.createEmptyBorder(2,2,2,2)));
		
        JPanel elementNumber = new JPanel();
        elementNumber.setLayout(new BoxLayout(elementNumber, BoxLayout.Y_AXIS));
        elementNumber.setLayout(new GridLayout(2,1,2,2));
        elementNumber.setMaximumSize(new Dimension(500,200));
        elementNumber.add(new JLabel(parent.getResourceBundle().getString("dElmTypeDialog13")));
		eNumber  = new DecimalField(eTypeNE, 3, numberFormat);
        elementNumber.add(eNumber);
        loftingRegion.add(elementNumber);
		
        meshPane.add(elementType);
		if ( nCurves == 2 ) {
	        meshPane.add(loftingRegion);
		}
		
		String label = parent.getResourceBundle().getString("dElmTypeDialog14") + lb;
        addTab(label, meshPane);
		
    } // End of constructor
    
    public int showDialog() {
        return super.showDialog(parent, parent.getResourceBundle().getString("dElmTypeDialog15"));
    }
	
    /**
     * This method is executed on OK button click.
     *
     * @param e The action event who called the execution.
     */
    public void approvedPerform() {
		try {
			
			if (lagran.isSelected()) {
				eTypeLS = "Lagrange";
			} else {
				eTypeLS = "Serendipity";
			}
			
			if (typeT.isSelected()) {
				eTypeTQ = "Triangulate";
			} else {
				eTypeTQ = "Quadrilateral";
			}
			
			eTypeNN = nodesNumber.getValue();
			
			if ( nCurves == 2 ) {
				eTypeNE = eNumber.getValue();
			}
			
		} catch (Exception err) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
		
    }
	
    /**
     * Sets the number formater
     */
    private void setUpFormat() {
        numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
        numberFormat.setMaximumFractionDigits(3);
    } //End of setUpFormat
	
	public String getETypeLS() {
		return eTypeLS;
	}
	
	public String getETypeTQ() {
		return eTypeTQ;
	}
	
	public int getETypeNN() {
		return (int)eTypeNN;
	}
	
	public int getETypeNE() {
		return (int)eTypeNE;
	}
	
	public boolean getLinearDivisor() {
		return linearDv.isSelected();
	}
	
	// private inner class to handle radio button events
	private class RadioButtonHandler implements ItemListener {
		
		// handle radio button events
		public void itemStateChanged( ItemEvent event ) {
			// user clicked plainButton
			if ( event.getSource() == typeQ ) {
				labelTQ.setText(parent.getResourceBundle().getString("dElmTypeDialog16"));
				linearDv.setSelected(false);
				linearDv.setEnabled(false);
			} else { // user clicked boldButton
				labelTQ.setText(parent.getResourceBundle().getString("dElmTypeDialog17"));
				linearDv.setEnabled(true);
			}
		}	
	}
}